
/* @Author      zouzh
 * @PackageName demo0
 * @Package     PACKAGE_NAME
 * @Date        2023/10/9 15:54
 */

import com.sun.org.apache.bcel.internal.ExceptionConstants;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static java.lang.Thread.sleep;

public class demo {
    public static void main(String[] args) throws InterruptedException, IOException {
//        System.out.println("你好");
//        text01();
//        text02();
//        text06();

//        text07();
//        text08();
//        text09();
        text12();
    }

    private static void text12() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("测试");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File screenshotAs = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotAs, new File("D://123456.png"));
    }

    private static void text09() throws InterruptedException {
        // 创建驱动
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 搜素 520
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        // 点击百度一下
        sleep(3000);
        webDriver.findElement(By.cssSelector("#su")).click();
        // 找到图片元素
        WebElement element = webDriver.findElement(By.cssSelector("#su"));
        // 右键

        Actions actions = new Actions(webDriver);
        sleep(3000);
        actions.contextClick(element).perform();


    }

    private static void text08() throws InterruptedException {
        // 创建驱动
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 搜素 521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        sleep(3000);
        // 点击百度一下
        webDriver.findElement(By.cssSelector("#su")).click();
        // 全选
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        // 剪切
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "x");
        // 粘贴
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");


    }

    private static void text07() throws InterruptedException {
        // 创建驱动
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 搜素 521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        sleep(3000);
        // 点击百度一下
        webDriver.findElement(By.cssSelector("#su")).click();
        // 浏览器后退
        sleep(3000);
        webDriver.navigate().back();
        // 浏览器刷新
        sleep(3000);
        webDriver.navigate().refresh();
        // 浏览器前进
        sleep(3000);
        webDriver.navigate().forward();
        // 清空输入框
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).clear();
        sleep(3000);
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
    }

    private static void text06() {
        // 创建驱动
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(webDriver, 3000);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(8) > span")));
    }

    private static void text02() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote--allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
    }

    private static void text01() throws InterruptedException {

        boolean flag = false;


        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote--allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com");

        // 找到百度搜索输入框
//        WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        element.sendKeys("测试");

        // 找到百度一下按钮并点击
        webDriver.findElement(By.cssSelector("#su")).click();
        // 休眠三秒
        sleep(3000);
        // 找到搜素结果并检验
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elements.size(); i++){
//            System.out.println(elements.get(i).getText());
            if (elements.get(i).getText().equals("测试")){
                flag = true;
                System.out.println("测试通过");
                break;
            }
        }
        if (flag == false){
            System.out.println("测试未通过");
        }
        // 休眠三秒
        sleep(3000);
        // 清空输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();

    }
}
